package com.yanwq.sayhi.common.tool;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.widget.Toast;

import com.theartofdev.edmodo.cropper.CropImage;
import com.theartofdev.edmodo.cropper.CropImageView;
import com.yanwq.sayhi.R;

import java.io.File;
import java.util.ArrayList;

import me.crosswall.photo.pick.PickConfig;

/**
 * 作者：yanweiqiang。
 * 日期：2016/7/25 0025。
 */
public class CropTool {

    public void pick(Context context) {
        new PickConfig.Builder((Activity) context)
                .pickMode(PickConfig.MODE_SINGLE_PICK)
                .maxPickSize(30)
                .spanCount(3)
                .toolbarColor(R.color.blue)
                .build();
    }

    public void pick(Context context, Fragment fragment) {
        new PickConfig.Builder(context, fragment)
                .pickMode(PickConfig.MODE_SINGLE_PICK)
                .maxPickSize(30)
                .spanCount(3)
                .toolbarColor(R.color.blue)
                .buildFragment();
    }

    public void crop(File desFile, Context context, int requestCode, int resultCode, Intent data, CropCallback callback) {
        if (requestCode == PickConfig.PICK_REQUEST_CODE) {
            if (resultCode == Activity.RESULT_OK) {
                ArrayList<String> pick = data.getStringArrayListExtra(PickConfig.EXTRA_STRING_ARRAYLIST);
                CropImage.activity(Uri.fromFile(new File(pick.get(0))))
                        .setOutputUri(Uri.fromFile(desFile))
                        .setGuidelines(CropImageView.Guidelines.ON)
                        .setOutputCompressQuality(80)
                        .setAspectRatio(1, 1)
                        .setFixAspectRatio(true)
                        .start((Activity) context);
            }
        }

        if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
            CropImage.ActivityResult result = CropImage.getActivityResult(data);
            if (resultCode == Activity.RESULT_OK) {
                Uri resultUri = result.getUri();
                if (callback != null) {
                    callback.onComplete(resultUri);
                }
            } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                Exception error = result.getError();
                Toast.makeText(context, error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }
    }

    public void crop(File desFile, Context context, Fragment fragment, int requestCode, int resultCode, Intent data, CropCallback callback) {
        if (requestCode == PickConfig.PICK_REQUEST_CODE) {
            if (resultCode == Activity.RESULT_OK) {
                ArrayList<String> pick = data.getStringArrayListExtra(PickConfig.EXTRA_STRING_ARRAYLIST);
                CropImage.activity(Uri.fromFile(new File(pick.get(0))))
                        .setOutputUri(Uri.fromFile(desFile))
                        .setGuidelines(CropImageView.Guidelines.ON)
                        .setOutputCompressQuality(80)
                        .setAspectRatio(1, 1)
                        .setFixAspectRatio(true)
                        .start(context, fragment);
            }
        }

        if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
            CropImage.ActivityResult result = CropImage.getActivityResult(data);
            if (resultCode == Activity.RESULT_OK) {
                Uri resultUri = result.getUri();
                if (callback != null) {
                    callback.onComplete(resultUri);
                }
            } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                Exception error = result.getError();
                Toast.makeText(context, error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }
    }

    public interface CropCallback {
        void onComplete(Uri resultUri);
    }
}
